package cn.kgm.user.server.abs.impl;

import cn.kgm.core.exception.BizException;
import cn.kgm.user.dto.constant.UserConstant;
import cn.kgm.user.dto.dto.UserRegisterDTO;
import cn.kgm.user.dto.entity.User;
import cn.kgm.user.dto.entity.UserTypeSys;
import cn.kgm.user.dto.vo.UserTypeSysVO;
import cn.kgm.user.server.abs.UserAbs;
import cn.kgm.user.server.service.UserTypeSysService;
import cn.kgm.user.server.struct.UserTypeSysMappers;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Optional;

/**
 * #技术改变自己
 *
 * @author: kappi
 * @email: yolo.jk@qq.com
 * @date: 2024/4/9 22:54周二
 * @package: cn.kgm.user.server.abs.impl.UserTypeSys
 */
@Slf4j
@Service(value = UserConstant.IocName.USER_SYS)
public class UserTypeSysAbsImpl extends UserAbs {

    @Resource
    private UserTypeSysService userSysService;

    @Override
    public User register(UserRegisterDTO dto) {
        User user = super.register(dto);
        UserTypeSysVO userSysVO = Optional.of(dto)
                .map(UserRegisterDTO::getUserTypeSys)
                .orElseThrow(() -> new BizException("param lose [UserRegisterDTO.UserTypeSys]"));
        UserTypeSys userTypeSys = UserTypeSysMappers.INSTANCE.mapToEntity(userSysVO);
        userTypeSys.setUserId(user.getId());
        userSysService.save(userTypeSys);
        return user;
    }
}
